package com.wondertek.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.wondertek.common.util.security.LoginUser;
import com.wondertek.common.util.security.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.Date;


/**
 * @author xqs
 */
@Slf4j
@Configuration
//@MapperScan(basePackages = { "com.wondertek.**"})
//@MapperScan(basePackages = { "com.wondertek.**.mapper" })
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Slf4j
    @Component
    static class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

        @Override
        public void insertFill(MetaObject metaObject) {
            LoginUser loginUser = SecurityUtil.getDefaultLoginUser();
            this.fillStrategy(metaObject, "createTime", new Date());
            this.fillStrategy(metaObject, "createBy", loginUser.getUsername());
        }

        @Override
        public void updateFill(MetaObject metaObject) {;
            LoginUser loginUser = SecurityUtil.getDefaultLoginUser();
            this.fillStrategy(metaObject, "updateTime", new Date());
            this.fillStrategy(metaObject, "updateBy", loginUser.getUsername());
        }

    }

}
